package com.sh.data.engine.web.webapi.controller.base.response;

import com.sh.data.engine.common.constants.HttpStatus;
import com.sh.data.engine.common.constants.code.BizCodes;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.io.Serializable;

/**
 * 接口返回的统一响应体
 *
 * @param <T>
 */
@Schema
@Data
public class RestResponseEntity<T> implements Serializable {

    @Schema(description = "响应码", example = "200", requiredMode = Schema.RequiredMode.REQUIRED)
    private int code = BizCodes.SUCCESS.getCode();

    @Schema(
        description = "请求响应状态 200-成功 500-失败 403-未登录 401-未授权",
        example = "200",
        requiredMode = Schema.RequiredMode.REQUIRED)
    private int status = HttpStatus.OK.value();

    @Schema(description = "请求响应消息", example = "请求成功", requiredMode = Schema.RequiredMode.REQUIRED)
    private String msg = "请求成功";

    @Schema(description = "请求响应结果")
    private T data = null;

    public RestResponseEntity() {
    }

    public RestResponseEntity(T data) {
        this.data = data;
    }

    public RestResponseEntity(String message, T data) {
        this.msg = message;
        this.data = data;
    }

    public RestResponseEntity(int status, String message) {
        this.status = status;
        this.msg = message;
    }

    public RestResponseEntity(int code, int status, String msg) {
        this.code = code;
        this.status = status;
        this.msg = msg;
    }

    public RestResponseEntity(int status, String message, T data) {
        this.status = status;
        this.msg = message;
        this.data = data;
    }

    public RestResponseEntity(int code, int status, String msg, T data) {
        this.code = code;
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    public static <T> RestResponseEntity<T> success(T data) {
        RestResponseEntity<T> restResponseEntity = new RestResponseEntity<>();
        restResponseEntity.setData(data);
        return restResponseEntity;
    }

    public static <T> RestResponseEntity<T> success() {
        return new RestResponseEntity<>();
    }

    public static <T> RestResponseEntity<T> error(String msg) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static <T> RestResponseEntity<T> error(int status, String msg) {
        return error(BizCodes.INTERNAL_SERVER_ERROR.getCode(), status, msg);
    }

    public static <T> RestResponseEntity<T> error(int code, int status, String msg) {
        RestResponseEntity<T> restResponseEntity = new RestResponseEntity<>();
        restResponseEntity.setCode(code);
        restResponseEntity.setMsg(msg);
        restResponseEntity.setStatus(status);
        return restResponseEntity;
    }
}
